{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 导入数据集"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "(2000, 21)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "phone = np.loadtxt(\"F:/机器学习数据集/phone_train.csv\",skiprows=1,delimiter=',')\n",
    "phone.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T05:41:54.768783600Z",
     "start_time": "2023-10-31T05:41:54.559796300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[8.420e+02, 0.000e+00, 2.200e+00, 0.000e+00, 1.000e+00, 0.000e+00,\n        7.000e+00, 6.000e-01, 1.880e+02, 2.000e+00, 2.000e+00, 2.000e+01,\n        7.560e+02, 2.549e+03, 9.000e+00, 7.000e+00, 1.900e+01, 0.000e+00,\n        0.000e+00, 1.000e+00, 1.000e+00],\n       [1.021e+03, 1.000e+00, 5.000e-01, 1.000e+00, 0.000e+00, 1.000e+00,\n        5.300e+01, 7.000e-01, 1.360e+02, 3.000e+00, 6.000e+00, 9.050e+02,\n        1.988e+03, 2.631e+03, 1.700e+01, 3.000e+00, 7.000e+00, 1.000e+00,\n        1.000e+00, 0.000e+00, 2.000e+00],\n       [5.630e+02, 1.000e+00, 5.000e-01, 1.000e+00, 2.000e+00, 1.000e+00,\n        4.100e+01, 9.000e-01, 1.450e+02, 5.000e+00, 6.000e+00, 1.263e+03,\n        1.716e+03, 2.603e+03, 1.100e+01, 2.000e+00, 9.000e+00, 1.000e+00,\n        1.000e+00, 0.000e+00, 2.000e+00],\n       [6.150e+02, 1.000e+00, 2.500e+00, 0.000e+00, 0.000e+00, 0.000e+00,\n        1.000e+01, 8.000e-01, 1.310e+02, 6.000e+00, 9.000e+00, 1.216e+03,\n        1.786e+03, 2.769e+03, 1.600e+01, 8.000e+00, 1.100e+01, 1.000e+00,\n        0.000e+00, 0.000e+00, 2.000e+00],\n       [1.821e+03, 1.000e+00, 1.200e+00, 0.000e+00, 1.300e+01, 1.000e+00,\n        4.400e+01, 6.000e-01, 1.410e+02, 2.000e+00, 1.400e+01, 1.208e+03,\n        1.212e+03, 1.411e+03, 8.000e+00, 2.000e+00, 1.500e+01, 1.000e+00,\n        1.000e+00, 0.000e+00, 1.000e+00]])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phone[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T05:42:11.887781400Z",
     "start_time": "2023-10-31T05:42:11.868783900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8.420e+02 0.000e+00 2.200e+00 0.000e+00 1.000e+00 0.000e+00 7.000e+00\n",
      "  6.000e-01 1.880e+02 2.000e+00 2.000e+00 2.000e+01 7.560e+02 2.549e+03\n",
      "  9.000e+00 7.000e+00 1.900e+01 0.000e+00 0.000e+00]\n",
      " [1.021e+03 1.000e+00 5.000e-01 1.000e+00 0.000e+00 1.000e+00 5.300e+01\n",
      "  7.000e-01 1.360e+02 3.000e+00 6.000e+00 9.050e+02 1.988e+03 2.631e+03\n",
      "  1.700e+01 3.000e+00 7.000e+00 1.000e+00 1.000e+00]\n",
      " [5.630e+02 1.000e+00 5.000e-01 1.000e+00 2.000e+00 1.000e+00 4.100e+01\n",
      "  9.000e-01 1.450e+02 5.000e+00 6.000e+00 1.263e+03 1.716e+03 2.603e+03\n",
      "  1.100e+01 2.000e+00 9.000e+00 1.000e+00 1.000e+00]\n",
      " [6.150e+02 1.000e+00 2.500e+00 0.000e+00 0.000e+00 0.000e+00 1.000e+01\n",
      "  8.000e-01 1.310e+02 6.000e+00 9.000e+00 1.216e+03 1.786e+03 2.769e+03\n",
      "  1.600e+01 8.000e+00 1.100e+01 1.000e+00 0.000e+00]\n",
      " [1.821e+03 1.000e+00 1.200e+00 0.000e+00 1.300e+01 1.000e+00 4.400e+01\n",
      "  6.000e-01 1.410e+02 2.000e+00 1.400e+01 1.208e+03 1.212e+03 1.411e+03\n",
      "  8.000e+00 2.000e+00 1.500e+01 1.000e+00 1.000e+00]] [1. 2. 2. 2. 1.]\n"
     ]
    }
   ],
   "source": [
    "X,y = phone[:,0:19],phone[:,20]\n",
    "print(X[:5],y[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T05:47:10.856781200Z",
     "start_time": "2023-10-31T05:47:10.824784200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "((1500, 19), (1500,), (500, 19), (500,))"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,shuffle=True)\n",
    "X_train.shape,y_train.shape,X_test.shape,y_test.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T05:51:00.701786200Z",
     "start_time": "2023-10-31T05:51:00.691789200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 随机森林"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:87.600%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "rf = RandomForestClassifier()\n",
    "rf.fit(X_train,y_train)\n",
    "print('测试集精度:{:.3f}%'.format(100*rf.score(X_test,y_test)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T05:56:28.808534Z",
     "start_time": "2023-10-31T05:56:28.399537600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'rf__max_depth': 5, 'rf__n_estimators': 100}\n",
      "最佳验证集精度:83.200%\n",
      "测试集精度:83.400%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.pipeline import Pipeline\n",
    "params_grid ={'rf__max_depth':[3,4,5],'rf__n_estimators':[75,100,125,150]}\n",
    "\n",
    "pipe = Pipeline([('scaler',MinMaxScaler()),('rf',RandomForestClassifier())])\n",
    "grid_rf = GridSearchCV(pipe,n_jobs=-1,param_grid=params_grid,cv=10)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print('最佳参数组合:',grid_rf.best_params_)\n",
    "print('最佳验证集精度:{:.3f}%'.format(grid_rf.best_score_*100))\n",
    "rf = RandomForestClassifier(n_estimators=150,max_depth=5)\n",
    "pipe_rf = Pipeline([('scaler',MinMaxScaler()),('rf',rf)])\n",
    "pipe_rf.fit(X_train,y_train)\n",
    "print('测试集精度:{:.3f}%'.format(pipe_rf.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T06:41:43.476599600Z",
     "start_time": "2023-10-31T06:41:33.588296800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 神经网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:93.000%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "pipe = Pipeline([('scaler',MinMaxScaler()),('mlp',MLPClassifier(max_iter=1200))])\n",
    "pipe.fit(X_train,y_train)\n",
    "print('测试集精度:{:.3f}%'.format(pipe.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T06:48:18.345628900Z",
     "start_time": "2023-10-31T06:48:08.931624500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合 {'mlp__learning_rate_init': 0.0005}\n",
      "最佳验证集精度92.333%\n",
      "测试集精度:93.000%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\myAnaconda\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "params_grid = {'mlp__learning_rate_init':[0.0005,0.001,0.01,1]}\n",
    "pipe = Pipeline([('scaler',MinMaxScaler()),('mlp',MLPClassifier(max_iter=1000))])\n",
    "grid_mlp = GridSearchCV(pipe,param_grid=params_grid,n_jobs=-1)\n",
    "grid_mlp.fit(X_train,y_train)\n",
    "print('最佳参数组合',grid_mlp.best_params_)\n",
    "print('最佳验证集精度{:.3f}%'.format(grid_mlp.best_score_*100))\n",
    "print('测试集精度:{:.3f}%'.format(grid_mlp.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T06:59:57.149258Z",
     "start_time": "2023-10-31T06:59:23.533357600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:94.200%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\myAnaconda\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "pipe_mlp = Pipeline([('scaler',MinMaxScaler()),('mlp',MLPClassifier(max_iter=1000,learning_rate_init=0.0005))])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "print(\"测试集精度:{:.3f}%\".format(pipe_mlp.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:02:36.498062900Z",
     "start_time": "2023-10-31T07:02:26.089062500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:85.200%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "pipe = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "pipe.fit(X_train,y_train)\n",
    "print('测试集精度:{:.3f}%'.format(pipe.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:15:55.564869200Z",
     "start_time": "2023-10-31T07:15:55.427896300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳验证集精度: {'svc__C': 10, 'svc__gamma': 0.01}\n",
      "最佳测试集精度:92.133%\n",
      "测试集精度:91.800%\n"
     ]
    }
   ],
   "source": [
    "pipe = Pipeline([('scaler',MinMaxScaler()),('svc',SVC())])\n",
    "params_grid = {'svc__gamma':[0.001,0.01,0.1,1],'svc__C':[0.001,0.01,0.1,1,10]}\n",
    "grid_svc = GridSearchCV(pipe,param_grid=params_grid,n_jobs=-1,cv=10)\n",
    "grid_svc.fit(X_train,y_train)\n",
    "print('最佳验证集精度:',grid_svc.best_params_)\n",
    "print('最佳测试集精度:{:.3f}%'.format(grid_svc.best_score_*100))\n",
    "print('测试集精度:{:.3f}%'.format(grid_svc.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:36:38.476250500Z",
     "start_time": "2023-10-31T07:36:33.525247800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度91.800%\n"
     ]
    }
   ],
   "source": [
    "pipe_svc = Pipeline([('scaler',MinMaxScaler()),('svc',SVC(gamma=0.01,C=10))])\n",
    "pipe_svc.fit(X_train,y_train)\n",
    "print('测试集精度{:.3f}%'.format(pipe_svc.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:47:08.329419900Z",
     "start_time": "2023-10-31T07:47:08.206438600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度99.950%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\myAnaconda\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1000) reached and the optimization hasn't converged yet.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "pipe_mlp.fit(X,y) # 组合训练其他数据\n",
    "print('测试集精度{:.3f}%'.format(pipe_mlp.score(X,y)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:49:20.642814900Z",
     "start_time": "2023-10-31T07:49:05.330261600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 3., 0., 0., 0.])"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe_mlp.predict(X[5:10])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T07:52:29.041364900Z",
     "start_time": "2023-10-31T07:52:29.022372100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [],
   "source": [
    "## 持久化模型\n",
    "import pickle\n",
    "import os\n",
    "try:\n",
    "    outFile = open(os.path.join(os.path.curdir,'phoneClassifier.clf'),mode='wb')\n",
    "    pickle.dump(pipe_mlp,outFile)\n",
    "except Exception as e:\n",
    "    # 处理错误过程\n",
    "    print('Error:',e)\n",
    "finally:\n",
    "    if outFile:\n",
    "        outFile.close() # 真正保存模型于文件中"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T08:09:46.264924400Z",
     "start_time": "2023-10-31T08:09:46.159936200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [],
   "source": [
    "try:\n",
    "    inFile = open(os.path.join(os.path.curdir,'phoneClassifier.clf'),mode='rb')\n",
    "    clf3 = pickle.load(inFile)\n",
    "except Exception as e:\n",
    "    print('错误:',e)\n",
    "finally:\n",
    "    if inFile:\n",
    "        inFile.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T08:14:40.716324Z",
     "start_time": "2023-10-31T08:14:40.555468200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 2., 2., 2., 1.])"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf3.predict(X[:5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-31T08:14:52.151095Z",
     "start_time": "2023-10-31T08:14:52.122040500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
